package cn.ibizlab.assetmanagement.assetapi.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.plan.domain.EAMPlanSchedule;
import cn.ibizlab.assetmanagement.core.plan.service.IEAMPlanScheduleService;
import cn.ibizlab.assetmanagement.core.plan.filter.EAMPlanScheduleSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护计划时刻设置" })
@RestController("AssetApi-eamplanschedule")
@RequestMapping("")
public class EAMPlanScheduleResource {

    @Autowired
    public IEAMPlanScheduleService eamplanscheduleService;

    @Autowired
    @Lazy
    public EAMPlanScheduleMapping eamplanscheduleMapping;

    @PreAuthorize("hasPermission(this.eamplanscheduleMapping.toDomain(#eamplanscheduledto),'iBizAssetManagement-EAMPlanSchedule-Create')")
    @ApiOperation(value = "新建维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "新建维护计划时刻设置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplanschedules")
    @Transactional
    public ResponseEntity<EAMPlanScheduleDTO> create(@RequestBody EAMPlanScheduleDTO eamplanscheduledto) {
        EAMPlanSchedule domain = eamplanscheduleMapping.toDomain(eamplanscheduledto);
		eamplanscheduleService.create(domain);
        EAMPlanScheduleDTO dto = eamplanscheduleMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanscheduleMapping.toDomain(#eamplanscheduledtos),'iBizAssetManagement-EAMPlanSchedule-Create')")
    @ApiOperation(value = "批量新建维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "批量新建维护计划时刻设置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplanschedules/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMPlanScheduleDTO> eamplanscheduledtos) {
        eamplanscheduleService.createBatch(eamplanscheduleMapping.toDomain(eamplanscheduledtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamplanschedule" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamplanscheduleService.get(#eamplanschedule_id),'iBizAssetManagement-EAMPlanSchedule-Update')")
    @ApiOperation(value = "更新维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "更新维护计划时刻设置")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamplanschedules/{eamplanschedule_id}")
    @Transactional
    public ResponseEntity<EAMPlanScheduleDTO> update(@PathVariable("eamplanschedule_id") String eamplanschedule_id, @RequestBody EAMPlanScheduleDTO eamplanscheduledto) {
		EAMPlanSchedule domain  = eamplanscheduleMapping.toDomain(eamplanscheduledto);
        domain .setEamplanscheduleid(eamplanschedule_id);
		eamplanscheduleService.update(domain );
		EAMPlanScheduleDTO dto = eamplanscheduleMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamplanscheduleService.getEamplanscheduleByEntities(this.eamplanscheduleMapping.toDomain(#eamplanscheduledtos)),'iBizAssetManagement-EAMPlanSchedule-Update')")
    @ApiOperation(value = "批量更新维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "批量更新维护计划时刻设置")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamplanschedules/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMPlanScheduleDTO> eamplanscheduledtos) {
        eamplanscheduleService.updateBatch(eamplanscheduleMapping.toDomain(eamplanscheduledtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamplanscheduleService.get(#eamplanschedule_id),'iBizAssetManagement-EAMPlanSchedule-Remove')")
    @ApiOperation(value = "删除维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "删除维护计划时刻设置")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamplanschedules/{eamplanschedule_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamplanschedule_id") String eamplanschedule_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamplanscheduleService.remove(eamplanschedule_id));
    }

    @PreAuthorize("hasPermission(this.eamplanscheduleService.getEamplanscheduleByIds(#ids),'iBizAssetManagement-EAMPlanSchedule-Remove')")
    @ApiOperation(value = "批量删除维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "批量删除维护计划时刻设置")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamplanschedules/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamplanscheduleService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamplanscheduleMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMPlanSchedule-Get')")
    @ApiOperation(value = "获取维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "获取维护计划时刻设置")
	@RequestMapping(method = RequestMethod.GET, value = "/eamplanschedules/{eamplanschedule_id}")
    public ResponseEntity<EAMPlanScheduleDTO> get(@PathVariable("eamplanschedule_id") String eamplanschedule_id) {
        EAMPlanSchedule domain = eamplanscheduleService.get(eamplanschedule_id);
        EAMPlanScheduleDTO dto = eamplanscheduleMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护计划时刻设置草稿", tags = {"维护计划时刻设置" },  notes = "获取维护计划时刻设置草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamplanschedules/getdraft")
    public ResponseEntity<EAMPlanScheduleDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamplanscheduleMapping.toDto(eamplanscheduleService.getDraft(new EAMPlanSchedule())));
    }

    @ApiOperation(value = "检查维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "检查维护计划时刻设置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplanschedules/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMPlanScheduleDTO eamplanscheduledto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamplanscheduleService.checkKey(eamplanscheduleMapping.toDomain(eamplanscheduledto)));
    }

    @PreAuthorize("hasPermission(this.eamplanscheduleMapping.toDomain(#eamplanscheduledto),'iBizAssetManagement-EAMPlanSchedule-Save')")
    @ApiOperation(value = "保存维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "保存维护计划时刻设置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplanschedules/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMPlanScheduleDTO eamplanscheduledto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamplanscheduleService.save(eamplanscheduleMapping.toDomain(eamplanscheduledto)));
    }

    @PreAuthorize("hasPermission(this.eamplanscheduleMapping.toDomain(#eamplanscheduledtos),'iBizAssetManagement-EAMPlanSchedule-Save')")
    @ApiOperation(value = "批量保存维护计划时刻设置", tags = {"维护计划时刻设置" },  notes = "批量保存维护计划时刻设置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamplanschedules/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMPlanScheduleDTO> eamplanscheduledtos) {
        eamplanscheduleService.saveBatch(eamplanscheduleMapping.toDomain(eamplanscheduledtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlanSchedule-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlanSchedule-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"维护计划时刻设置" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamplanschedules/fetchdefault")
	public ResponseEntity<List<EAMPlanScheduleDTO>> fetchDefault(EAMPlanScheduleSearchContext context) {
        Page<EAMPlanSchedule> domains = eamplanscheduleService.searchDefault(context) ;
        List<EAMPlanScheduleDTO> list = eamplanscheduleMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPlanSchedule-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPlanSchedule-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"维护计划时刻设置" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamplanschedules/searchdefault")
	public ResponseEntity<Page<EAMPlanScheduleDTO>> searchDefault(@RequestBody EAMPlanScheduleSearchContext context) {
        Page<EAMPlanSchedule> domains = eamplanscheduleService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamplanscheduleMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

